﻿//--------------------------------------------------------------------
// 动态报表控件基类 v1.00
//
// 开发人： 朱明智
// 创建时间： 2019-08-22
//
// 修改人：朱明智
// 修改时间：2020-06-28
//--------------------------------------------------------------------

namespace Dynamic.Controller
{
    /// <summary>
    /// 控件基类
    /// </summary>
    public abstract class ReportController
    {
        /// <summary>
        /// 父控件
        /// </summary>
        public ReportController Parent
        {
            get;
            internal set;
        }

        /// <summary>
        /// 获取报表
        /// </summary>
        public DynamicReport Report
        {
            get
            {
                for (ReportController p = this.Parent; p != null; p = p.Parent)
                {
                    if (p is DynamicReport)
                    {
                        return (DynamicReport)p;
                    }
                }

                return null;
            }
        }

        /// <summary>
        /// 模板字符串
        /// </summary>
        protected string template;

        /// <summary>
        /// 控件上边距离 cm
        /// </summary>
        protected float top;

        /// <summary>
        /// 控件距离左边距离 cm
        /// </summary>
        protected float left;

        /// <summary>
        /// 控件高 CM
        /// </summary>
        protected float height;

        /// <summary>
        /// 控件宽 CM
        /// </summary>
        protected float width;

        public ReportController()
        {
        }

        /// <summary>
        /// 根据标签名，和单位大小生成标签字符 例如： <![CDATA[<Width>10cm<Width>]]> 
        /// </summary>
        /// <param name="length">长度，单位CM</param>
        /// <param name="labelName">标签名称</param>
        /// <returns></returns>
        private static string GetLabelStr(float length, string labelName)
        {
            return length == 0f ? "" : string.Format("<{0}>{1}cm</{0}>", labelName, length);
        }

        /// <summary>
        /// 获取控件的XML文本
        /// </summary>
        /// <returns>XML文本</returns>
        public string GetTempStr()
        {
            return ToString();
        }

        public override string ToString()
        {
            return template.Replace("@Top", GetLabelStr(top, "Top"))
                .Replace("@Left", GetLabelStr(left, "Left"))
                .Replace("@Height", GetLabelStr(height, "Height"))
                .Replace("@Width", GetLabelStr(width, "Width"));
        }
    }
}